# 全局变量
# 系统数据库，系统预先保存的卡号、密码和余额
card_id = [6212872083082357, 6212872083082358, 6212872083082359, 6212872083082360, 6212872083082371]
card_paswd = [653130, 653121, 650101, 651324, 653031]
card_over = {6212872083082357: 500, 6212872083082358: 2000, 6212872083082359: 6000, 6212872083082360: 100,
             6212872083082371: 2}
# 用户输入时保存数据的临时变量
pd = 0  # 用户输入的密码
item = 0  # 卡号对应的下标
Success = False  # 认证结果
state = True  # 用户的卡是否在取款机


# 查询余额函数
def refer():
    if Success:
        print("你的余额", card_over[card_id[item]])
        ATM()
    else:
        print("你还没有验证密码")
        ATM_exit()


# 退出系统操作
def ATM_exit():
    global pd, item, card_id, card_paswd, card_over, Success, state
    if not state:
        pd = 0  # 用户输入的密码
        item = 0  # 卡号对应的下标
        Success = False  # 认证结果
        ka()
    else:
        ATM()


# 取款操作函数
def draw_money():
    global pd, item, card_id, card_paswd, card_over, Success, state
    print("温情提示，取款机单次最高取款2000元，选择其他金额时以100元整数倍输入")
    print("{0:1} {1:14} {2:10}".format(" ", "1. 100", "2. 300"))
    print("{0:1} {1:14} {2:10}".format(" ", "3. 500", "4. 1000"))
    print("{0:1} {1:14} {2:10}".format(" ", "5. 2000", "6. 其他"))
    print("{0:1} {1:14} {2:10}".format(" ", "7. 退出", " "))
    user_ATM = int(input())
    if user_ATM == 1:
        if card_over[card_id[item]] >= 100:
            card_over[card_id[item]] = card_over[card_id[item]] - 100
            print("请取回你的钱")
            refer()
            draw_money()
        else:
            print("你的存款金额少于取款金额！")
            ATM_exit()
    elif user_ATM == 2:
        if card_over[card_id[item]] >= 300:
            card_over[card_id[item]] = card_over[card_id[item]] - 300
            print("请取回你的钱")
            refer()
            draw_money()
        else:
            print("你的存款金额少于取款金额！")
            refer()
            draw_money()
    elif user_ATM == 3:
        if card_over[card_id[item]] >= 500:
            card_over[card_id[item]] = card_over[card_id[item]] - 500
            print("请取回你的钱")
            refer()
            draw_money()
        else:
            print("你的存款金额少于取款金额！")
            refer()
            draw_money()
    elif user_ATM == 4:
        if card_over[card_id[item]] >= 1000:
            card_over[card_id[item]] = card_over[card_id[item]] - 1000
            print("请取回你的钱")
            refer()
            draw_money()
        else:
            print("你的存款金额少于取款金额！")
            refer()
            draw_money()
    elif user_ATM == 5:
        if card_over[card_id[item]] >= 2000:
            card_over[card_id[item]] = card_over[card_id[item]] - 2000
            print("请取回你的钱")
            refer()
            draw_money()
        else:
            print("你的存款金额少于取款金额！")
            refer()
            draw_money()
    elif user_ATM == 6:
        user_input = int(input("请输入你要取款的金额："))
        if 2000 >= user_input >= card_over[card_id[item]]:
            card_over[card_id[item]] = card_over[card_id[item]] - user_input
            print("请取回你的钱")
            refer()
            draw_money()
        else:
            if user_input > 2000:
                print("输入有效金额")
                refer()
                draw_money()
            else:
                print("你的存款金额少于取款金额！")
                refer()
                draw_money()
    elif user_ATM == 7:
        ATM_exit()
    else:
        print("无效的选择")
        draw_money()


# 存款操作
def save_money():
    global pd, item, card_id, card_paswd, card_over, Success, state
    print("温情提示，取款机单次最高可存入2000元，请放入以100元钞票")
    user_input = int(input("请存入你的钱"))
    card_over[card_id[item]] = card_over[card_id[item]] + user_input
    print("{0:1} {1:14} {2:10}".format(" ", "1. 继续存款", "2. 退出"))
    user_input1 = int(input("请选择："))
    if user_input1 == 1:
        save_money()
    elif user_input1 == 2:
        ATM()
    else:
        print("无效选择")
        ATM()


# 修改密码操作
def change_password():
    global pd, item, card_id, card_paswd, card_over, Success, state
    user_input = int(input("请输入原密码："))
    if user_input == card_paswd[item]:
        user_newpd1 = int(input("请输入新的密码："))
        user_newpd2 = int(input("请再输入一次新的密码："))
        if user_newpd1 == user_newpd2:
            card_paswd[item] = user_newpd2
            print("修改密码成功")
            ATM()
        else:
            print("你输入的密码不一致")
            change_password()
    else:
        print("原密码不正确，请收回你的卡片")
        state = False
        ATM_exit()


# 转账操作
def transfer():
    print("温情提示，每个用户每次最高转账限制50000元")
    user_input = int(input("请输入转账卡号："))
    if user_input in card_id:
        user_money = int(input("请输入转账金额:"))
        if user_money <= 50000 and user_money <= card_over[card_id[item]]:
            card_over[card_id[item]] = card_over[card_id[item]] - user_money
            card_over[user_input] = card_over[user_input] + user_money
            print("转账成功，你的转账金额为：{:}".format(user_money))
            ATM()
        else:
            if 50000 < user_money:
                print("转账金额超出每天单次转账限制")
                transfer()
            else:
                print("你的余额不足")
                ATM()


# 验证卡号和密码是否匹配，作为简单的认证
def verification(kid):
    global pd, item, card_id, card_paswd, card_over, Success, state
    if kid in card_id:
        print("请输入6位数字密码，输入密码时注意周围环境是否安全！")
        pd = int(input("请输入密码:"))
        item = card_id.index(kid)
        if pd == card_paswd[item]:
            Success = True
            ATM()
    else:
        print("无效的卡号")
        ka()
    return Success


def ATM():
    global pd, item, card_id, card_paswd, card_over, Success, state
    if Success:
        print("=" * 12, "欢迎使用工商银行ATM自动取款系统", "=" * 14)
        print("{0:1} {1:14} {2:10}".format(" ", "1. 查询余额", "2. 取款"))
        print("{0:1} {1:15} {2:10}".format(" ", "3. 存款", "4. 修改密码"))
        print("{0:1} {1:15} {2:10}".format(" ", "5. 转账", "6. 退卡"))
        print("=" * 46)
        user_operation = int(input())
        if user_operation == 1:  # 显示用户余额
            refer()
        elif user_operation == 2:  # 进入取款页面并且进行取款操作
            draw_money()
        elif user_operation == 3:  # 进入存款页面进行操作
            save_money()
        elif user_operation == 4:  # 进入用户修改密码页面进行修改密码操作
            change_password()
        elif user_operation == 5:  # 进入转账页面进行转账操作
            transfer()
        elif user_operation == 6:  # 退卡卡操作
            print("请取回你的卡")
            state = False
            ATM_exit()
        else:
            print("无效选择")
            ATM()
    else:
        print("密码无效")
        state = False
        ATM_exit()


def ka():
    # 因本系统没有可以直接插入卡获取卡号的设备，临时提醒显示用户输入自己的卡号
    print("=" * 12, "欢迎使用工商银行ATM机请插卡", "=" * 14)
    print("{0:1} {1:15} {2:10}".format(" ", card_id[0], card_id[1]))
    print("{0:1} {1:15} {2:10}".format(" ", card_id[2], card_id[3]))
    print("{0:1} {1:15} {2:10}".format(" ", card_id[4], " "))
    print("=" * 12, "*" * 30, "=" * 14)
    user_input = int(input("请输入你的卡号："))
    if user_input in card_id:
        verification(user_input)
    else:
        print("卡号无效")
        ka()


ka()
